<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/16
 * Time: 18:10
 */

namespace App\Process;


use App\Sock\Logic\Game;
use App\Utility\HttpRequest;
use App\Utility\MTool;
use App\Utility\UUID;
use EasySwoole\Core\Component\Cache\Cache;
use EasySwoole\Core\Component\Di;
use EasySwoole\Core\Swoole\Process\AbstractProcess;
use EasySwoole\Core\Swoole\Time\Timer;
use Swoole\Exception;
use Swoole\Process;
use EasySwoole\Config;
use App\Sock\Logic\Game as GameLogic;
use EasySwoole\Core\Swoole\ServerManager;

class Heartbeat extends AbstractProcess
{

    protected $db;

    public function __construct(string $processName, array $args, bool $async = true)
    {
        $dbConf = Config::getInstance()->getConf('database');
        $this->db = Di::getInstance()->get('MYSQL');
        $this->db->setPrefix($dbConf['prefix']);
        parent::__construct($processName, $args, $async);
    }

    public function run(Process $process)
    {

        Timer::loop(10 * 1000,function(){
            $room_list = Cache::getInstance()->get('room_list');
            foreach ($room_list as $item => $value) {
                $user[] = GameLogic::selectRoomFd($value['room_uuid']);
            }
            if (!empty($user)) {
                if (count($user) == 1) {
                    $users = $user[0];
                } else {
                    $users = [];
                    foreach ($user as $items => $list) {
                        foreach ($list as $ii => $vv) {
                            $users[] = $vv;
                        }
                    }
                }
            }

            $server = ServerManager::getInstance()->getServer();
            foreach ($users as $userFd) {
                $connection = $server->connection_info($userFd);
                if ($connection['websocket_status'] == 3) { // 用户正常在线时可以进行消息推送
                    $server->push($userFd, json_encode(['type'=> 'logout']));
                }else{
                    GameLogic::recyclingFd($userFd);
                }
            }
        });

    }


    protected
    function sendMessage($taskData = [])
    {
        $users = [];
        $roomid = isset($taskData['room_id']) ? $taskData['room_id'] : 0;
        if (empty($roomid)) {
            $room_list = Cache::getInstance()->get('room_list');
            foreach ($room_list as $item => $value) {
                $user[] = GameLogic::selectRoomFd($value['room_uuid']);
            }
            if (!empty($user)) {
                if (count($user) == 1) {
                    $users = $user[0];
                } else {
                    $users = [];
                    foreach ($user as $items => $list) {
                        foreach ($list as $ii => $vv) {
                            $users[] = $vv;
                        }
                    }
                }
            }


        } else {
            $users = GameLogic::selectRoomFd($roomid);
        }

        /** @var \swoole_websocket_server $server */
        $server = ServerManager::getInstance()->getServer();
        foreach ($users as $userFd) {
            $connection = $server->connection_info($userFd);
            if ($connection['websocket_status'] == 3) { // 用户正常在线时可以进行消息推送
                $server->push($userFd, $taskData['payload']);
            }
        }
    }


    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }
}